package com.jy.adscm.operation.aftersales.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.aftersales.bean.QueryShRepair;
import com.jy.adscm.operation.aftersales.bean.QueryShRepairUpkeep;
import com.jy.adscm.operation.aftersales.bean.ShRepair;
import com.jy.adscm.operation.aftersales.bean.ShRepairUpkeep;
import com.jy.adscm.operation.aftersales.service.ShRepairService;
import com.jy.adscm.operation.aftersales.service.ShRepairUpkeepService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-08-28
 */
@RestController
@RequestMapping("/adscm/operation/aftersales/shRepairUpkeep")
public class ShRepairUpkeepController {
    @Autowired
    ShRepairUpkeepService shRepairUpkeepService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;


    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody ShRepairUpkeep shRepairUpkeep, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        shRepairUpkeep.setCreator(token.getUser().getId().toString());
        shRepairUpkeep.setCreateTime(new Date());
        boolean insert = shRepairUpkeepService.insertShRepairUpkeep(shRepairUpkeep);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody ShRepairUpkeep shRepairUpkeep, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = shRepairUpkeepService.updateShRepairUpkeep(shRepairUpkeep,token);
        return  new SimpleCoreResponse("200","修改成功",insert);
    }
    @RequestMapping("select")
    public SimpleCoreResponse select(@RequestBody QueryShRepairUpkeep queryShRepairUpkeep, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Page<ShRepairUpkeep> page=new Page<>(queryShRepairUpkeep.getCurrentPage(),queryShRepairUpkeep.getPageSize());
        page= shRepairUpkeepService.selectShRepairUpkeep(page,queryShRepairUpkeep);
        return  new SimpleCoreResponse("200","查询成功",page);
    }

    @RequestMapping("selectShRepairUpkeepPersons")
    public SimpleCoreResponse selectShRepairUpkeepPersons(HttpServletRequest request)throws AdscmSystemException {
        List<JyUser> jyUsers = shRepairUpkeepService.selectShRepairUpkeepPersons();
        return  new SimpleCoreResponse("200","查询成功",jyUsers);
    }
}

